home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.10 Oct 96 / AdownsJavaObjects / ToolPalette.java < prev   
Encoding:
Text File  |  1996-09-11  |  5.7 KB  |  217 lines  |  [TEXT/R*ch]

  1. /*
  2.  
  3. Listing 4: ToolPalette .java
  4.  
  5. ToolPalette.java
  6. Define and create our tool palette.
  7.  
  8. */
  9.  
  10. // Import the generic Java classes.
  11. import java.applet.*;
  12. import java.awt.*;
  13. import java.io.*;
  14. import java.lang.*;
  15.  
  16. //    Our class definition.
  17. public    class ToolPalette extends Frame
  18. {
  19.     static        int            maxItems = 9;
  20.     int                theItemColorArray[] = new int[ maxItems ];
  21.     Button        bQuit, bAddRect, bAddButton, bDelete;
  22.     Choice        colorMenu = new Choice();        
  23.     Choice        itemMenu = new Choice();        
  24.     Demo            myDemo;
  25.     Frame            theToolPalette;
  26.     Label            thePanelLabel = new Label();
  27.     Label            theColorLabel = new Label( "Color:" );
  28.     Label            theItemLabel = new Label( "Item:" );
  29.     MenuBar        theButtonMenuBar;
  30.     Panel            pButtonPalette;
  31.     Panel            pColorPalette;
  32.     Panel            pBlankPanel;
  33.  
  34.  
  35.     //    The constructor for our class.
  36.     public ToolPalette( Demo thisDemo )
  37.     {
  38.         //    Variables we need for initialization.
  39.         int                defaultX = 20, defaultY = 100;
  40.         int                defaultWidth = 300, defaultHeight = 170;
  41.         int                i = 0;
  42.         Rectangle    theBounds = new Rectangle();
  43.         String        theStringBlack = new String( "Black" );
  44.         String        theStringGreen = new String( "Green" );
  45.         String        theStringPink = new String( "Pink" );
  46.         String        theStringBlue = new String( "Blue" );
  47.         String        theStringLightGray = new String( 
  48.                                     "Light Gray" );
  49.         String        theStringRed = new String( "Red" );
  50.         String        theStringCyan = new String( "Cyan" );
  51.         String        theStringMagenta = new String( "Magenta" );
  52.         String        theStringWhite = new String( "White" );
  53.         String        theStringDarkGray = new String( "Dark Gray" );
  54.         String        theStringOrange = new String( "Orange" );
  55.         String        theStringYellow = new String( "Yellow" );
  56.         String        theStringGray = new String( "Gray" );
  57.         String        theStringAppWindow = new String( 
  58.                                     "Applet window" );
  59.         String        theStringRectBack = new String( 
  60.                                     "Panel background" );
  61.         String        theStringRectText = new String( 
  62.                                     "Panel label" );
  63.         String        theStringButBack = new String( 
  64.                                     "Button background" );
  65.         String        theStringButText = new String( 
  66.                                     "Button label" );
  67.  
  68.         myDemo = thisDemo;
  69.  
  70.         pButtonPalette = new Panel();
  71.         pButtonPalette.setLayout( new GridLayout( 3, 3 ) );
  72.  
  73.         bQuit = new Button();
  74.         bAddRect = new Button();
  75.         bAddButton = new Button();
  76.         bDelete = new Button();
  77.  
  78.         bQuit.setLabel( "Quit" );
  79.         bAddRect.setLabel( "Add Panel" );
  80.         bAddButton.setLabel( "Add Button..." );
  81.         bDelete.setLabel( "Delete Item" );
  82.  
  83.         pButtonPalette.add( bAddRect );
  84.         pButtonPalette.add( bAddButton );
  85.         pButtonPalette.add( bDelete );
  86.         pButtonPalette.add( bQuit );
  87.         pButtonPalette.add( new Label() );
  88.         pButtonPalette.add( new Label() );
  89.         pButtonPalette.add( new Label() );
  90.  
  91.         theItemColorArray[ 0 ] = 12;
  92.  
  93.         for ( i = 1; i < maxItems; i++ )
  94.         {
  95.             theItemColorArray[ i ] = i + 1;
  96.         }
  97.  
  98.         colorMenu.addItem( theStringBlack );
  99.         colorMenu.addItem( theStringGreen );
  100.         colorMenu.addItem( theStringPink );
  101.         colorMenu.addItem( theStringBlue );
  102.         colorMenu.addItem( theStringLightGray );
  103.         colorMenu.addItem( theStringRed );
  104.         colorMenu.addItem( theStringCyan );
  105.         colorMenu.addItem( theStringMagenta );
  106.         colorMenu.addItem( theStringWhite );
  107.         colorMenu.addItem( theStringDarkGray );
  108.         colorMenu.addItem( theStringOrange );
  109.         colorMenu.addItem( theStringYellow );
  110.         colorMenu.addItem( theStringGray );
  111.  
  112.         itemMenu.addItem( theStringAppWindow );
  113.         itemMenu.addItem( theStringRectBack );
  114.         itemMenu.addItem( theStringRectText );
  115.         itemMenu.addItem( theStringButBack );
  116.         itemMenu.addItem( theStringButText );
  117.  
  118.         pColorPalette = new Panel();
  119.         pColorPalette.setLayout( new GridLayout( 4, 2 ) );
  120.  
  121.         pColorPalette.add( theColorLabel );
  122.         pColorPalette.add( colorMenu );
  123.         pColorPalette.add( new Label() );
  124.         pColorPalette.add( new Label() );
  125.         pColorPalette.add( theItemLabel );
  126.         pColorPalette.add( itemMenu );
  127.         pColorPalette.add( new Label() );
  128.         pColorPalette.add( new Label() );
  129.  
  130.         itemMenu.select( 0 );
  131.         colorMenu.select( theItemColorArray[ 0 ] );
  132.         
  133.         pBlankPanel = new Panel();
  134.         pBlankPanel.setLayout( new FlowLayout() );
  135.         theBounds = pBlankPanel.bounds();
  136.         pBlankPanel.reshape( theBounds.x, theBounds.y, 20, 20 );
  137.  
  138.         setBackground( Color.lightGray );
  139.         setMenuBar( theButtonMenuBar );
  140.         setResizable( true );
  141.         setTitle( "Tool Palette" );
  142.         reshape( defaultX, defaultY, defaultWidth, 
  143.                 defaultHeight );
  144.         add( "North", pButtonPalette );
  145.         add( "Center", pColorPalette );
  146.  
  147.         show();
  148.     }
  149.     
  150.     //    Override the action() method of our parent class.
  151.     public boolean action( Event evt, Object arg )
  152.     {
  153.         boolean        eventHandled = false;
  154.         int                theColor = 0, theItem = 0;
  155.         Object        theTarget;
  156.  
  157.         if ( evt.target instanceof Choice )
  158.         {
  159.             theTarget = ( Choice )evt.target;
  160.  
  161.             if ( theTarget == itemMenu )
  162.             {
  163.                 theItem = 
  164.                         ( ( Choice )evt.target ).getSelectedIndex();
  165.                 theColor = theItemColorArray[ theItem ];
  166.                 colorMenu.select( theColor );
  167.  
  168.                 eventHandled = true;
  169.             }
  170.  
  171.             if ( theTarget == colorMenu )
  172.             {
  173.                 theColor = 
  174.                         ( ( Choice )evt.target ).getSelectedIndex();
  175.                 theItem = itemMenu.getSelectedIndex();
  176.                 theItemColorArray[ theItem ] = theColor;
  177.  
  178.                 if ( theItem == 0 )
  179.                     eventHandled = myDemo.doSetBackColor( theColor );
  180.                 else
  181.                     eventHandled = 
  182.                             myDemo.doChangeColor( theItem, theColor );
  183.  
  184.                 eventHandled = true;
  185.             }
  186.         }
  187.         else if ( evt.target instanceof Button )
  188.         {
  189.             theTarget = ( Button )evt.target;
  190.  
  191.             if ( theTarget == bQuit )
  192.                 myDemo.doDestroy();
  193.  
  194.             if ( theTarget == bAddRect )
  195.                 eventHandled = myDemo.doAddRect();
  196.  
  197.             if ( theTarget == bAddButton )
  198.                 eventHandled = myDemo.doCreateButtonFrame();
  199.  
  200.             if ( theTarget == bDelete )
  201.                 eventHandled = myDemo.doDeleteItem();
  202.         }
  203.  
  204.         if ( eventHandled )
  205.             return true;
  206.         else
  207.             return false;
  208.     }
  209.  
  210.     //    Return an int representing the color of the requested item.
  211.     public int doGetColor( int itemNo )
  212.     {
  213.         return theItemColorArray[ itemNo ];
  214.     }
  215.  
  216. }
  217.